package com.eliza.spring.cloud.nacos.provider.controller

import com.eliza.spring.cloud.library.pojo.Order
import com.eliza.spring.cloud.library.pojo.User
import com.eliza.spring.cloud.nacos.provider.service.OrderService
import com.eliza.spring.library.utils.ToolsLog
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.client.RestTemplate
import javax.annotation.Resource

@Controller
@RequestMapping("/order")
class OrderController {
    @Resource
    private lateinit var orderService: OrderService

    /*
    * 需要单独新建配置项
    * */
    @Autowired
    private lateinit var restTemplate: RestTemplate

    //通过负载均衡发现地址，流程是从服务发现中心拿  nacosConsumer 服务的列表，通过负载均衡算法获取一个地址
    @Resource
    private lateinit var loadBalancerClient: LoadBalancerClient

    @ResponseBody
    @GetMapping("/getService/{orderId}")
    fun sdasd(@PathVariable("orderId") orderId: Long) {

        ToolsLog.LogPrintln("Info:", 16)
        ToolsLog.LogPrintln("Info:${loadBalancerClient.choose("nacosProvider").uri}", 16)
        ToolsLog.LogPrintln("Info2:${loadBalancerClient.choose("nacosConsumer").uri}", 14)

        val order = orderService.queryOrderById(orderId)


        /**
        * 加入负载均衡后 不能直接诶使用 ip地址，而应该使用 nacos 服务注册名称
        * */
        val serviceId = "nacosConsumer"
        val uri = loadBalancerClient.choose(serviceId).uri
//        val final = "$uri/user/${order?.userId ?: 1}"
        val final = "http://nacosConsumer/user/${order?.userId ?: 1}"
        ToolsLog.LogPrintln("FinalUrl: $final", 49)
        ToolsLog.LogPrintln("User: ${restTemplate.getForObject(final, User::class.java)}", 11)


    }


    @ResponseBody
    @GetMapping("/getAll")
    fun getAll(): String {
        // 根据id查询订单并返回
        return "${orderService.getAllOrder()}"
    }

    @ResponseBody
    @GetMapping("/getAll/{orderId}")

    fun getUser(@PathVariable("orderId") orderId: Long): User? {
        // 根据id查询订单并返回

        val order = orderService.queryOrderById(orderId)
        ToolsLog.LogPrintln(order, 12)

        val forObject =
//            restTemplate.getForObject("http://192.168.20.3:8080/user/" + (order?.userId ?: 1), User::class.java)
            restTemplate.getForObject("http://nacosConsumer/user/" + (order?.userId ?: 1), User::class.java)
        ToolsLog.LogPrintln(forObject, 49)
        return forObject

    }


    //    consumer
    @GetMapping("/{orderId}")
    @ResponseBody

    fun queryOrderAndUserByOrderId(@PathVariable("orderId") orderId: Long): Order? {
        // 根据id查询订单并返回
        //return orderService.queryOrderById(orderId);
        return orderService.queryOrderAndUserByOrderId(orderId)
    }

}